//
//  MOMyShenPiViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/9/20.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOMyShenPiViewController.h"
#import "MOCommonUseViewCell.h"
#import "MOMyShenPiWebViewViewController.h"

@interface MOMyShenPiViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)UICollectionView *collectionView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation MOMyShenPiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    
    [self requestData];
}

- (void)initView{
    self.title = @"我的审批";
    self.view.backgroundColor = KGrayColor_ededed;
    [self.view addSubview:self.collectionView];
}

-(void)requestData
{
    self.dataArray = [NSMutableArray array];
    
    NSArray *nameArr = @[@[@"流程列表"],@[@"公车申请",@"IT维修申请",@"会议室申请"]];
    NSArray *imageArr = @[@[@"liuChengLieVBiao"],@[@"gongCheShenQing",@"ITWeiXiu",@"huiYiShiShenQing",@"huiYiShiShenQing"]];
    for (int i = 0; i<nameArr.count; i++) {
        NSArray *arr = nameArr[i];
        NSMutableArray *muArray = [NSMutableArray array];
        for (int j = 0; j<arr.count; j++) {
            MOCommonUseModel *model = [[MOCommonUseModel alloc]init];
            model.nameStr = arr[j];
            model.imageStr = imageArr[i][j];
            model.itemIndex = i;
            model.typeStr = @"3";
            [muArray addObject:model];
        }
        [self.dataArray addObject:muArray];
    }
    [self.collectionView reloadData];
}

#pragma mark -UICollectionViewDataSource,UICollectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.dataArray.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    NSArray *arr = self.dataArray[section];
    return arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MOCommonUseViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.section][indexPath.row];
    return cell;
    
}
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(SCREEN_WIDTH/3, SCREEN_WIDTH/3);
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
    
}

//定义每个UICollectionView 横向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}

//定义每个UICollectionView 竖向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    if (section==0) {
        return CGSizeMake(SCREEN_WIDTH,0.0001);
    }else{
        return CGSizeMake(SCREEN_WIDTH,kHConversions(52));
    }

}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        if (kind == UICollectionElementKindSectionHeader) {
            
            UICollectionReusableView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
            headerView.backgroundColor = [UIColor whiteColor];
            
            return headerView;
            
        }
        return nil;
    }else{
        if (kind == UICollectionElementKindSectionHeader) {
            
            UICollectionReusableView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
            headerView.backgroundColor = [UIColor whiteColor];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH - 30, kHConversions(52))];
            label.font = FONT_SIZE(15);
            label.textColor = KBlackColor_222222;
            label.text = @"审批工单";
            [headerView addSubview:label];
            
            UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
            lineView.backgroundColor = KGrayColor_ededed;
            [headerView addSubview:lineView];
            
            return headerView;
            
        }
        return nil;
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *urlStr = @"";
    NSString *titleStr = @"";
    NSString *rolenumbers =[MOUser sharedMOUser].rolenumbers;
    if (rolenumbers.length==0) {
        [[ADShowMessage shareShowMessage]showTextOnly:@"请重新登录" showInView:self.view];
        return;
    }
    if (indexPath.section==0) {
        if (indexPath.item==0) {//流程列表
            urlStr = [NSString stringWithFormat:@"%@/static/dynamic-form/show-todo.html?staffId=%@&group=%@",AD_HOME,[MOUser sharedMOUser].userName,rolenumbers];
            titleStr = @"流程管理";
        }
        NSLog(@"%@",urlStr);
        MOMyShenPiWebViewViewController *vc = [[MOMyShenPiWebViewViewController alloc]init];
        vc.urlStr = urlStr;
        vc.titleStr = titleStr;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        
        [self gotoShenPiDetailAndIndex:indexPath.item];
        
    }
    
}

-(void)gotoShenPiDetailAndIndex:(NSInteger)item
{
    NSString *processKeyStr = @"";
    NSString *titleStr = @"";
    titleStr = @"工单申请";
    if (item==0) {//公车申请
        processKeyStr = @"useCarApply";
    }else if (item==1){//IT维修
        processKeyStr = @"fixApply";
    }else if (item==2){//会议室申请
        processKeyStr = @"meeting_room";
    }
    NSString *postUrl = [NSString stringWithFormat:@"%@?processKey=%@",STARTPROC_STARTPROCESS_URL,processKeyStr];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:postUrl parameters:nil success:^(id responseObject) {
        NSLog(@"工单申请：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        NSString *urlStr = [NSString stringWithFormat:@"%@/static/dynamic-form/show-page.html?staffId=%@&processDefinitionId=%@&proKey=%@",AD_HOME,[MOUser sharedMOUser].userName,responseObject,processKeyStr];
        if ([processKeyStr isEqualToString:@"KEY_publish"]) {
            urlStr = [NSString stringWithFormat:@"%@&jnTitlez=%@",urlStr,@"局内发布"];
        }
        NSLog(@"%@",urlStr);
        MOMyShenPiWebViewViewController *vc = [[MOMyShenPiWebViewViewController alloc]init];
        vc.urlStr = urlStr;
        vc.titleStr = titleStr;
        [self.navigationController pushViewController:vc animated:YES];
        
    } failure:^(NSError *error) {
        NSLog(@"工单申请：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
    }];
    

}


#pragma mark -懒加载
-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,1, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_collectionView registerClass:[MOCommonUseViewCell class] forCellWithReuseIdentifier:@"cell"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    }
    return _collectionView;
}


@end
